home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / grepfv10.zip / GREPFV10.C < prev    next >
C/C++ Source or Header  |  1994-03-15  |  21KB  |  815 lines

  1. /* GREPFV10 - Microsoft C source
  2. Find regular expression in files and archives
  3. =======================================================
  4. Nigel Salt
  5. 25 Lower Station Rd
  6. Crayford
  7. Kent
  8. DA1 3PY
  9. UK
  10.  
  11. Phone +44 322 553260
  12.  
  13. Email nao@cix.compulink.co.uk
  14. =======================================================
  15. Can process PAK ZIP ARC ARJ LZH and ZOO files
  16.    grepfv  expression filepattern[+] tempdir   
  17.    MSoft C V6   
  18.    [Options]
  19.    Argv 1 regular expression
  20.         2 file pattern
  21.         3 Path for temporary files
  22.    FV.COM must be in path along with any necessary
  23.    utilities to unarchive files
  24.    + on the end of file pattern activates recursive directory search
  25. */
  26. /**********************************************************************
  27.    The getfiles function is recursive if search of subdirectories
  28.    is specified.
  29.    This can cause stack overflow which may be remedied by compiling as
  30.    cl -F D000 grepfv.c
  31.    This gives the program a very large stack
  32. ***********************************************************************/
  33. #include <ctype.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <process.h>
  37. #include <dos.h>
  38. #include <string.h>
  39. #include <time.h>
  40.  
  41. /* Prototypes */
  42. int ifile(char *fname);
  43. int xfile(char *fname);
  44. void dosearch(char *name);
  45. void doarc(char *arcname);
  46. void dofile(char *fname);
  47. void usage(char *error);
  48. char *regstr(char *regexp,char *string);
  49. int regpars(char *r);
  50. void nsfgets(char *buff,int max,FILE *f);
  51. void getfiles(char *p,char *n,int all);
  52. void dosummary(void);
  53.  
  54. /* Global variables */
  55. /* Structure to hold regular expression segments */
  56. typedef struct
  57. {
  58.   char op;
  59.   char *cptr;
  60. } REGSEG;
  61. REGSEG regsegs[64];
  62. int numexp;
  63.  
  64. /* Structure to hold file exclusion  / inclusion strings 
  65.    sptr points to a string
  66.    next is NULL or a pointer to the next item in the list */
  67.  
  68. typedef struct lstruct
  69. {
  70.   char *sptr;
  71.   struct lstruct *next;
  72. } LTYPE, *LTYPEPTR;
  73. static LTYPE xlist;
  74. static LTYPE ilist;
  75.  
  76. static char tempath[256];
  77. static char zippath[256];
  78. static char buff[256];
  79. static char regexp[256];
  80. static char version[]="GREPFV10";
  81. static char copyright[]=" (c) Nigel Salt 1994";
  82. static int  checksum=1422;
  83. static unsigned long  numfound=0;
  84. static unsigned long  numarcs=0;
  85. static unsigned long  numfiles=0;
  86. static unsigned long  numfailed=0;
  87. static unsigned long  numexc=0;
  88. static unsigned long  numchars=0;
  89. static time_t         startime;
  90. static time_t         endtime;
  91. static FILE *nsout=NULL;
  92. static char *outfilnam;
  93. static int  outfile=0;
  94.  
  95. void main(int argc,char *argv[])
  96. {
  97.   int all=0;
  98.   int pattend;
  99.   int nextarg=0;
  100.   char path[129];
  101.   char name[65];
  102.   char *fslash;
  103.   char *lastslash;
  104.   char *s;
  105.   int count;
  106.   char *arglist[256];
  107.   LTYPEPTR xcurptr,icurptr;
  108.   FILE *optfile;
  109.   int i;
  110.   int maxarg=0;
  111.   int firstarg=1;
  112.   char *tokadd;
  113.  
  114.  
  115.   /* Set start time in seconds */
  116.   time(&startime);
  117.  
  118.   
  119.  
  120.   /* Set include list to empty */
  121.   icurptr=&ilist;
  122.   icurptr->sptr=NULL;
  123.   icurptr->next=NULL;
  124.   
  125.   /* Set exclude list to empty */
  126.   xcurptr=&xlist;
  127.   xcurptr->sptr=NULL;
  128.   xcurptr->next=NULL;
  129.   
  130.   /* Check copyright notice */
  131.   s=copyright;
  132.   count=0;
  133.   while (*s)
  134.      count+=*s++;
  135.   if (count!=checksum)
  136.      {
  137.      fprintf(stderr,"\nCopyright violation");
  138.      exit(1);
  139.      }
  140.  
  141.   fprintf(stderr,"%s%s\n",version,copyright);
  142.  
  143.   /* Copy args to list */
  144.   if (argv[1][0]=='@')
  145.     {
  146.     firstarg=2;
  147.     optfile=fopen(&argv[1][1],"r");
  148.     if (optfile==NULL)
  149.       {
  150.       sprintf(buff,"Cannot open option file '%s'",&argv[1][1]);
  151.       usage(buff);
  152.       }
  153.     while (!feof(optfile) && maxarg<256)
  154.       {
  155.       if (fgets(buff,255,optfile)==NULL) continue;
  156.       if (feof(optfile)) continue;
  157.       tokadd=strtok(buff,"\n\r\t ");
  158.       while (tokadd!=NULL && maxarg<256)
  159.         {
  160.         if ( (arglist[maxarg]=calloc(1,strlen(tokadd)+1))==NULL)
  161.           usage("No memory for parameters from file");
  162.         strcpy(arglist[maxarg],tokadd);
  163.         maxarg++;
  164.         tokadd=strtok(NULL,"\n\r\t ");
  165.         }
  166.       }
  167.     fclose(optfile);
  168.     }
  169.   for (i=firstarg;i<argc;i++,maxarg++)
  170.     {
  171.     if (maxarg>255)
  172.       usage("Too many parameters - max is 255");
  173.     arglist[maxarg]=argv[i];
  174.     }
  175.  
  176.   if (maxarg<2)
  177.     usage("You must specify an expression and filepattern");
  178.  
  179.  
  180.  
  181.   /* Print command line for reference where results redirected to file */
  182.   while (arglist[nextarg][0]=='-')
  183.     {
  184.     switch (toupper(arglist[nextarg][1]))
  185.       {
  186.       case 'O':
  187.         nextarg++;
  188.         outfilnam=arglist[nextarg];
  189.         nsout=fopen(outfilnam,"r");
  190.         if (nsout!=NULL)
  191.           {
  192.           sprintf(buff,"Output file '%s' already exists!\nDelete it or choose a new output file name"
  193.             ,outfilnam);
  194.           usage(buff);
  195.           }
  196.         nsout=fopen(outfilnam,"w");
  197.         if (nsout==NULL)
  198.           {
  199.           sprintf(buff,"Cannot open output file '%s'",outfilnam);
  200.           usage(buff);
  201.           }
  202.         outfile=1;
  203.         nextarg++;
  204.         break;
  205.       case 'X':
  206.         nextarg++;
  207.         strupr(arglist[nextarg]);
  208.         xcurptr->next=(LTYPEPTR)(malloc(sizeof(LTYPE)));
  209.         if (xcurptr->next==NULL)
  210.           usage("Cannot allocate memory for exclude option");
  211.         xcurptr->sptr=arglist[nextarg];
  212.         nextarg++;
  213.         xcurptr->next->next=NULL;
  214.         xcurptr->next->sptr=NULL;
  215.         xcurptr=xcurptr->next;
  216.         break;
  217.       case 'I':
  218.         nextarg++;
  219.         strupr(arglist[nextarg]);
  220.         icurptr->next=(LTYPEPTR)(malloc(sizeof(LTYPE)));
  221.         if (icurptr->next==NULL)
  222.           usage("Cannot allocate memory for include option");
  223.         icurptr->sptr=arglist[nextarg];
  224.         nextarg++;
  225.         icurptr->next->next=NULL;
  226.         icurptr->next->sptr=NULL;
  227.         icurptr=icurptr->next;
  228.         break;
  229.       default:
  230.         sprintf(buff,"Unrecognised option '%s'",arglist[nextarg]);
  231.         usage(buff);
  232.       }
  233.     }
  234.  
  235.   /* Print options for reference */
  236.   fprintf(stdout,"GREPFV10 SEARCH PARAMETERS\nOPTIONS:");
  237.   if (outfile)
  238.     fprintf(nsout,"GREPFV10 SEARCH PARAMETERS\nOPTIONS:");
  239.   icurptr=&ilist;
  240.   while (icurptr->sptr!=NULL)
  241.     {
  242.     fprintf(stdout,"\n\t -i %s",icurptr->sptr);
  243.     if (outfile)
  244.       fprintf(nsout,"\n\t -i %s",icurptr->sptr);
  245.     icurptr=icurptr->next;
  246.     }
  247.   xcurptr=&xlist;
  248.   while (xcurptr->sptr!=NULL)
  249.     {
  250.     fprintf(stdout,"\n\t -x %s",xcurptr->sptr);
  251.     if (outfile)
  252.       fprintf(nsout,"\n\t -x %s",xcurptr->sptr);
  253.     xcurptr=xcurptr->next;
  254.     }
  255.   if (outfile)
  256.     {
  257.     fprintf(stdout,"\n\t -o %s",outfilnam);
  258.     fprintf(nsout,"\n\t -o %s",outfilnam);
  259.     }
  260.   /* Nextarg should now be set to the regexp parameter */
  261.   if (nextarg+1>maxarg)
  262.     usage("You must specify an expression and filepattern");
  263.  
  264.   /* Get the regular expression */
  265.   fprintf(stdout,"\nEXPRESSION:\t%s",arglist[nextarg]);
  266.   if (outfile)
  267.     fprintf(nsout,"\nEXPRESSION:\t%s",arglist[nextarg]);
  268.   strcpy(regexp,arglist[nextarg]);
  269.   strupr(regexp);
  270.   numexp=regpars(regexp);
  271.   nextarg++;
  272.  
  273.   /* Process the file pattern */
  274.   fprintf(stdout,"\nFILE PATTERN:\t%s",arglist[nextarg]);
  275.   if (outfile)
  276.     fprintf(nsout,"\nFILE PATTERN:\t%s",arglist[nextarg]);
  277.   strcpy(path,strupr(arglist[nextarg]));
  278.   nextarg++;
  279.   pattend=strlen(path)-1;
  280.   if (path[pattend]=='+')
  281.     {
  282.     all=1; /* + at end so recurse subdirectories */
  283.     path[pattend]='\0';
  284.     pattend--;
  285.     }
  286.  
  287.   /* Change forward slashes to backslashes */
  288.   fslash=strchr(path,'/');
  289.   while (fslash!=NULL)
  290.     {
  291.     *fslash='\\';
  292.     fslash=strchr(path,'/');
  293.     }
  294.  
  295.   /* Split name and path */
  296.   if (path[pattend]=='\\')
  297.     strcpy(name,"*.*");
  298.   else
  299.     {
  300.     lastslash=strrchr(path,'\\');
  301.     if (lastslash==NULL)
  302.       {
  303.       strcpy(name,path);
  304.       path[0]='\0';
  305.       }
  306.     else
  307.       {
  308.       strcpy(name,lastslash+1);
  309.       *(lastslash+1)='\0';
  310.       }
  311.     }
  312.  
  313.   
  314.   /* Process temporary file path if any */
  315.   if (maxarg>nextarg)
  316.     {
  317.     fprintf(stdout,"\nTEMP PATH:\t%s",arglist[nextarg]);
  318.